/**
 * @(#) EnrollmentController.java
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.cms.controller.enrollment;

import in.ac.dei.edrp.cms.dao.enrollment.EnrollmentService;
import in.ac.dei.edrp.cms.domain.enrollment.EnrollmentInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * this is Server side controller class for Enrollment
 *
 * @version 1.0 11 MAR 2011
 * @author MOHD AMIR
 */
public class EnrollmentController extends MultiActionController {
	/** creating object of enrollmentService interface */
	private EnrollmentService enrollmentService;

	/** defining setter method for object of EnrollmentService interface */
	public void setEnrollmentService(EnrollmentService enrollmentService) {
		this.enrollmentService = enrollmentService;
	}

	/**
	 * This method get personal details from database and map to a jsp
	 * @param request
	 * @param response
	 * @return ModelAndView containing personal details
	 */
	public ModelAndView getPersonalInfo(HttpServletRequest req,
			HttpServletResponse res) {
		List<EnrollmentInfo> details = enrollmentService
				.getPersonalDetails("REG11111");
		return new ModelAndView("enrollment/personalInfo", "details", details);
	}

	/**
	 * This method get academic details from database and map to a jsp
	 * @param request
	 * @param response
	 * @return ModelAndView containing academic details
	 */
	public ModelAndView getAcademicInfo(HttpServletRequest req,
			HttpServletResponse res) {
		List<EnrollmentInfo> details = enrollmentService
				.getAcademicDetails("REG11111");
		return new ModelAndView("enrollment/academicInfo", "details", details);
	}

	/**
	 * This method get list of addresses from database and map to a jsp
	 * @param request
	 * @param response
	 * @return ModelAndView containing addresses List
	 */
	public ModelAndView getContactInfo(HttpServletRequest req,
			HttpServletResponse res) {
		List<EnrollmentInfo> details = enrollmentService
				.getContactDetails("12");
		return new ModelAndView("enrollment/contactInfo", "details", details);
	}

	/**
	 * This method insert student info into database
	 * @param request
	 * @param response
	 * @return ModelAndView containing info (True/False)
	 */
	public ModelAndView setStudentInfo(HttpServletRequest req,
			HttpServletResponse res) {
		EnrollmentInfo personalInfo = new EnrollmentInfo();
		
		if(req.getParameter("status").equalsIgnoreCase("SUB"))
		{
			String date = (new java.sql.Timestamp(new Date().getTime())).toString().substring(0, 19);
			
			personalInfo.setSubmitDate(date);
		}

		personalInfo.setEnrollmentNo(req.getParameter("enrollmentNo"));
		personalInfo.setRegistrationNo("REG11111");
		personalInfo.setStudentId(req.getParameter("studentId"));
		personalInfo.setSessionEndDate("2011-12-31");
		personalInfo.setSessionStartDate("2011-07-01");
		personalInfo.setStatus(req.getParameter("status"));
		personalInfo.setStudentFirstName(req.getParameter("firstName"));
		personalInfo.setStudentMiddleName(req.getParameter("middleName"));
		personalInfo.setStudentLastName(req.getParameter("lastName"));
		personalInfo.setFatherFirstName(req.getParameter("fatherFirstName"));
		personalInfo
				.setFatherMiddleName(req.getParameter("fatherMiddleName"));
		personalInfo.setFatherLastName(req.getParameter("fatherLastName"));
		personalInfo.setMotherFirstName(req.getParameter("motherFirstName"));
		personalInfo
				.setMotherMiddleName(req.getParameter("motherMiddleName"));
		personalInfo.setMotherLastName(req.getParameter("motherLastName"));
		personalInfo.setCategoryCode(req.getParameter("category"));
		personalInfo.setReligion(req.getParameter("religion"));
		personalInfo.setNationality(req.getParameter("nationality"));
		personalInfo.setEntityCode(req.getParameter("entity"));
		personalInfo.setProgramCode(req.getParameter("program"));
		personalInfo.setBranchCode(req.getParameter("branch"));
		personalInfo
				.setSpecializationCode(req.getParameter("specialization"));
		personalInfo.setGuardianName(req.getParameter("guardian"));
		personalInfo.setDob(req.getParameter("dob"));
		personalInfo.setPrimaryMail(req.getParameter("primaryEmailId"));
		personalInfo.setSecondaryMail(req.getParameter("secondaryEmailId"));
		List<EnrollmentInfo> addressList=new ArrayList<EnrollmentInfo>();
		List<EnrollmentInfo> academicList=new ArrayList<EnrollmentInfo>();
		
		StringTokenizer addressToken=new StringTokenizer(req.getParameter("address"),"|");
		StringTokenizer cityToken=new StringTokenizer(req.getParameter("city"),"|");
		StringTokenizer stateToken=new StringTokenizer(req.getParameter("state"),"|");
		StringTokenizer pinToken=new StringTokenizer(req.getParameter("pin"),"|");
		int i=0;
		while(cityToken.hasMoreTokens())
		{
			EnrollmentInfo addressInfo=new EnrollmentInfo();
			if(i==0)
			{
				addressInfo.setAddressKey("PER");
				addressInfo.setOfficePhone(req.getParameter("officePhone"));
				addressInfo.setHomePhone(req.getParameter("homePhone"));
				addressInfo.setOtherPhone(req.getParameter("otherPhone"));
			}
			else
			{
				addressInfo.setAddressKey("COR");
			}
			addressInfo.setStudentId("12");
			addressInfo.setUserType("STD");
			addressInfo.setCity(cityToken.nextToken());
			addressInfo.setState(stateToken.nextToken());
			
			if(addressToken.hasMoreTokens())
			{
				addressInfo.setAddress(addressToken.nextToken());
			}
			
			if(pinToken.hasMoreTokens())
			{
				addressInfo.setPinCode(pinToken.nextToken());
			}
			
			addressList.add(addressInfo);
			i++;
		}
		
		StringTokenizer examToken=new StringTokenizer(req.getParameter("examCode"),"|");
		StringTokenizer yearToken=new StringTokenizer(req.getParameter("year"),"|");
		StringTokenizer boardToken=new StringTokenizer(req.getParameter("boardCode"),"|");
		StringTokenizer schoolCollegeToken=new StringTokenizer(req.getParameter("schoolCollege"),"|");
		StringTokenizer marksObtainedToken=new StringTokenizer(req.getParameter("marksObtained"),"|");
		StringTokenizer totalMarksToken=new StringTokenizer(req.getParameter("totalMarks"),"|");
		
		while(examToken.hasMoreTokens())
		{
			EnrollmentInfo academicInfo=new EnrollmentInfo();
			
			academicInfo.setRegistrationNo("REG11111");
			academicInfo.setBoardCode(boardToken.nextToken());
			academicInfo.setExamCode(examToken.nextToken());
			academicInfo.setYear(yearToken.nextToken());
			academicInfo.setTotalMarks(Integer.parseInt(totalMarksToken.nextToken()));
			academicInfo.setMarksObtained(Integer.parseInt(marksObtainedToken.nextToken()));
			academicInfo.setCollege(schoolCollegeToken.nextToken());
			
			academicList.add(academicInfo);
		}
		
		Boolean isInserted = enrollmentService.updateStudentDetails(personalInfo, addressList, academicList);
		return new ModelAndView("enrollment/info", "info",req.getParameter("status")+isInserted);
	}
}
